package com.papers.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.papers.common.annotation.RepeatSubmit;
import com.papers.common.annotation.Log;
import com.papers.common.core.controller.BaseController;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.domain.R;
import com.papers.common.core.validate.AddGroup;
import com.papers.common.core.validate.EditGroup;
import com.papers.common.core.validate.QueryGroup;
import com.papers.common.enums.BusinessType;
import com.papers.common.utils.poi.ExcelUtil;
import com.papers.system.domain.vo.TJuanUserCourseVo;
import com.papers.system.domain.bo.TJuanUserCourseBo;
import com.papers.system.service.ITJuanUserCourseService;
import com.papers.common.core.page.TableDataInfo;

/**
 * 用户课程关联
 *
 * @author papers
 * @date 2022-10-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/juanUserCourse")
public class TJuanUserCourseController extends BaseController {

    private final ITJuanUserCourseService iTJuanUserCourseService;

    /**
     * 查询用户课程关联列表
     */
    @SaCheckPermission("system:juanUserCourse:list")
    @GetMapping("/list")
    public TableDataInfo<TJuanUserCourseVo> list(TJuanUserCourseBo bo, PageQuery pageQuery) {
        return iTJuanUserCourseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户课程关联列表
     */
    @SaCheckPermission("system:juanUserCourse:export")
    @Log(title = "用户课程关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TJuanUserCourseBo bo, HttpServletResponse response) {
        List<TJuanUserCourseVo> list = iTJuanUserCourseService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户课程关联", TJuanUserCourseVo.class, response);
    }

    /**
     * 获取用户课程关联详细信息
     *
     * @param userId 主键
     */
    @SaCheckPermission("system:juanUserCourse:query")
    @GetMapping("/{userId}")
    public R<TJuanUserCourseVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String userId) {
        return R.ok(iTJuanUserCourseService.queryById(userId));
    }

    /**
     * 新增用户课程关联
     */
    @SaCheckPermission("system:juanUserCourse:add")
    @Log(title = "用户课程关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TJuanUserCourseBo bo) {
        return toAjax(iTJuanUserCourseService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改用户课程关联
     */
    @SaCheckPermission("system:juanUserCourse:edit")
    @Log(title = "用户课程关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TJuanUserCourseBo bo) {
        return toAjax(iTJuanUserCourseService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除用户课程关联
     *
     * @param userIds 主键串
     */
    @SaCheckPermission("system:juanUserCourse:remove")
    @Log(title = "用户课程关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] userIds) {
        return toAjax(iTJuanUserCourseService.deleteWithValidByIds(Arrays.asList(userIds), true) ? 1 : 0);
    }
}
